// 基于对象的栈数据结构
interface StackItems<T> extends Object{
    [index:number]:T
}
class Stack<T> {
    items: StackItems<T> = {};
    current:number = 0;
    constructor(...args:T[]){
        if(!args.length){
            return;
        }
        this.push(...args)

    }
    // push 推入一个或者多个数据
    push(...args:T[]):void{
        if(!args.length) return;
        for (let i = 0; i < args.length; i++) {
            this.current++
            this.items[this.current] = args[i];
        }
    }
    // pop 删除栈顶元素,并且返回删除的元素
    pop():T|undefined{
        if(!this.current){
            return undefined;
        }

        let popItem = this.items[this.current];
        delete this.items[this.current];
        this.current--;
        return popItem
    }

    // peek 查看栈顶元素
    peek():T|undefined{
        return this.current ? this.items[this.current] : undefined;
    }

    // clear 清空栈
    clear():void{
        this.items = {}
        this.current = 0;
    }
    size(){
        return this.current;
    }
    isEmpty(){
        return this.current===0;
    }
    toString(){
        if(this.isEmpty()) return "";
        let r_str = ""
        for (const item in this.items) {
            r_str += this.items[item];
            r_str+=","
        }

        r_str.substr(0,r_str.length-1)
    }
}

export default Stack;
